package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 添加品牌
     * @param brandDTO 品牌信息，包含所属分类信息
     * @return
     */
    @PostMapping
    public ResponseEntity<Void>  saveBrand(BrandDTO brandDTO){
        brandService.save(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新品牌
     * @param brandDTO 品牌信息，包含所属分类信息
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.update(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据品牌id集合查询品牌集合
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByBrandIds(@RequestParam("ids") List<Long> ids){
//        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(Arrays.asList(ids))));
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(ids)));
    }

    /**
     * 根据分类id查询品牌
     * @param cid 分类id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long cid){
        return ResponseEntity.ok(brandService.queryBrandByCategoryId(cid));
    }

    /**
     * 分页查询品牌
     * @param key 搜索关键字条件
     * @param curPage 当前页码
     * @param rows 每页大小
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPageAndCondition(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Long curPage,
            @RequestParam(value = "rows",defaultValue = "5")Long rows){

        return ResponseEntity.ok(brandService.queryBrandByPageAndCondition(key,curPage,rows));
    }

    /**
     * 根据id查询品牌
     * @param id 品牌id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){
        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id")Long id){
        brandService.deleteBrandById(id);
        return ResponseEntity.ok().build();
    }



}